package classes.view.component
{
	import classes.controller.DAOSendFlash;
	import classes.controller.EDataObject;
	import classes.controller.ENumber;
	
	import com.d4p.custom.defaultButton.DefaultBtn;
	import com.greensock.TweenLite;
	import com.greensock.easing.Cubic;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class PopupFriend_Add extends Sprite
	{
		private var _cate:PopupSelectBox;
		private var _nickInput:MovieClip;
		private var _searchBtn:DefaultBtn;
		private var _alt_subject:Sprite;
		
		public function PopupFriend_Add()
		{
			super();
			setLayOut();
			defaultSetting();
			init();
		}
		private function setLayOut():void
		{
			this._cate 					= this.getChildByName('cate_mc')			as PopupSelectBox;
			this._cate.viewWidth  		= 100;
			this._cate.firstTitle 		= "아이디"; 
			this._cate.colorNoChange 	= true;
			
			this._nickInput				= this.getChildByName('input_mc')			as MovieClip;
			this._searchBtn				= this.getChildByName('search_mc')			as DefaultBtn;;	
			this._alt_subject			= this.getChildByName('alt_subject_mc')		as Sprite;
		}
		
		private function init():void
		{
			this._cate.addEventListener(ENumber.EVENT_NUMBER,				onClickBox);
			this._searchBtn.addEventListener(MouseEvent.CLICK,				onClickHandler);
			this._alt_subject.addEventListener(MouseEvent.CLICK,			onClickSubjectAlert);
			this.addEventListener(Event.ADDED_TO_STAGE, onStageHandler);
		}
		private function onStageHandler(e:Event):void
		{
			stage.addEventListener(MouseEvent.CLICK, onStageClick); 
			this.removeEventListener(Event.ADDED_TO_STAGE, onStageHandler);
		}
		
		//상자 외부클릭시 모든 박스 닫기
		private function onStageClick(event:MouseEvent):void
		{
			if(!contains(event.target as DisplayObject)){
				_cate.selectClipSwitch = false;
			}
			
		}
		
		
		private function defaultSetting():void
		{
			this.visible 				= false;
			this.alpha   				= 0;
			this._alt_subject.visible 	= false;
			this._alt_subject.alpha 	= 0;
		}
		
		private function onClickHandler(e:MouseEvent):void
		{
			//trace(">>> friend search click");
			
			if(_nickInput.search_txt.text == ""){
				_alt_subject.y=35;
				TweenLite.to(_alt_subject, 0.3, {autoAlpha:1, y:41, ease:Cubic.easeOut});
			}else{
				var type:String;
				if(_cate.currentType == "아이디"){
					type = 'id';
				}else{
					type = 'nick';
				}
				
				var obj:Object = {
					"data":[
						{
							"type":type
							,"value":_nickInput.search_txt.text
						}
					]
				}
					
				DAOSendFlash.getLoadInstance().setData('friendSearchList', obj);
				if(_alt_subject.alpha != 0) TweenLite.to(_alt_subject, 0.3, {autoAlpha:0, ease:Cubic.easeOut});
				
			}
			//trace('>>>'+obj.data[0].searchKey + "____ " + obj.data[0].stx);
		}
		
		public function alertOff():void{_alt_subject.visible = false; _alt_subject.alpha = 0};
		
		private function onClickSubjectAlert(e:MouseEvent):void
		{
			TweenLite.to(_alt_subject, 0.3, {autoAlpha:0, ease:Cubic.easeOut});
		}
		
		private function onClickBox(e:ENumber):void
		{
			var arr:Array = [{id:0, name:"아이디"}, {id:1, name:"닉네임"}];
			this._cate.viewOn = arr;
		}
	}
}
